const mongoose = require('mongoose')
const {Schema} = require("mongoose");

mongoose.connect('mongodb://localhost:27017/blog', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

const userSchema = mongoose.Schema({
    username: String,
    password: String,
    joined_date: Date,
    fans: [{
        _id: {type: Schema.Types.ObjectId, ref: 'users'},
        username: {type: Schema.Types.String, ref: 'users'},
}],
    subs: [{
        _id: {type: Schema.Types.ObjectId, ref: 'users'},
        username: {type: Schema.Types.String, ref: 'users'},
    }],
});

const User = mongoose.model('users', userSchema)

const blogSchema = mongoose.Schema({
    username: String,
    avatar: String,
    title: String,
    content: String,
    publish_date: Date,
    comment: [{
        author: String,
        reply: String,
        content: String,
        created: Date,
    }],
})

const Blog = mongoose.model('blogs', blogSchema)

const submitSchema = mongoose.Schema({
    id: String,
    file_name: String,
    time_limit: Number,
    memory_limit: Number,
    cases: Number,
    code: String,
    state: Number,
});

const Submit = mongoose.model('submits', submitSchema)

const administratorSchema = mongoose.Schema({
    username: String,
    password: String,
});

const Administrator = mongoose.model('administrators', administratorSchema)

const questionSchema = mongoose.Schema({
    username: String,
    id: Number,
    title: String,
    avatar: String,
    content: String,
    time_limit: Number,
    memory_limit: Number,
    cases: Number,
    publish_date: Date,
})

const Question = mongoose.model('questions', questionSchema)

const answerSchema = mongoose.Schema({
    "ret": Number,
    "cases": Number,
    "id": String,
    "code": String,
    "file_name": Number,
})

const Answer = mongoose.model('answers', answerSchema)

module.exports = {
    User,
    Blog,
    Submit,
    Administrator,
    Question,
    Answer,
}